
from services.user_service import user_info_service
from services.weibo_service import weibo_info_month_ago_service
from services.ai_service import get_all_model_result, get_one_model_result
from services.fans_service import get_random_fans

from database.user_repo import select_user_by_user_id
from database.weibo_repo import count_activity_by_release_time


from models.user_info import UserInfo

from utils.date_format import is_over_one_day, get_last_month_date, get_current_date, get_last_week_date
from utils.print_message import print_message

from ai.model.qianfan import get_qianfan_result

"""
判断一个用户是否为僵尸粉需要以下数据：
1.用户基本信息
2.用户最近十条微博数据
3.用户近一个月的赞/转/评数
4.将数据整合给大模型进行分析
"""

def get_ai_input_data(user_id, web_cookie, phone_cookie):
    # 1.获取这个用户的基本信息
    user_info_data = select_user_by_user_id(user_id)
    # 1.1 如果没有或者数据超过一天,则需要重新爬取、存储
    if not user_info_data or is_over_one_day(user_info_data['update_time']):
        user_info_data = user_info_service(user_id, web_cookie)
    # 这里需要把数据库的数据转成能用的数据
    else:
        user_info_data = UserInfo(**user_info_data).json()
    print_message(f"user_info_data的id: {user_id}")
    # 2.获取用户最近十条微博数据
    weibo_info = weibo_info_month_ago_service(user_id, phone_cookie, limit = 10)
    if user_info_data['repost_count'] == 0 or len(weibo_info) == 0 or len(weibo_info) == 0:
        return {
            'user_info': user_info_data,
            'weibo_info': None,
            'activity_data': None
        }
    # 3.获取用户近一个月的赞/转/评数
    month_ago = get_last_month_date()
    week_ago = get_last_week_date()
    current = get_current_date()
    
    activity_week_ago_data = count_activity_by_release_time(user_id, week_ago, current)
    for key in activity_week_ago_data.keys():
        activity_week_ago_data[key] = int(activity_week_ago_data[key]) if activity_week_ago_data[key] else 0
    
    activity_month_ago_data = count_activity_by_release_time(user_id, month_ago, current)
    for key in activity_month_ago_data.keys():
        activity_month_ago_data[key] = int(activity_month_ago_data[key]) if activity_week_ago_data[key] else 0
    # 4.得到组合数据
    input_data = {
        'user_info': user_info_data,
        'weibo_info': weibo_info,
        'activity_data': {
            'week_ago': activity_week_ago_data,
            'month_ago': activity_month_ago_data
        }
    }
    return input_data

def analyse_user_service(user_id, cookie):
    input_data = get_ai_input_data(user_id, cookie)
    # 5.调用模型分析
    result = get_all_model_result(user_id, input_data)
    return result


def analyse_user_service_singal_ai(user_id, model_name, web_cookie, phone_cookie):
    input_data = get_ai_input_data(user_id, web_cookie, phone_cookie)
    result = get_one_model_result(user_id, model_name, input_data)
    return result

def analyse_fans_random(user_id, model_name, web_cookie, phone_cookie):
    fans_id_list = get_random_fans(user_id, web_cookie)
    if type(fans_id_list) == str:
        return fans_id_list
    result = []
    # 根据每个id去调用模型
    for fans_id in fans_id_list:
        input_data = get_ai_input_data(fans_id, web_cookie, phone_cookie)
        analyse_result = get_one_model_result(fans_id, model_name, input_data)
        if analyse_result == None:
            continue
        analyse_result['user_info'] = input_data['user_info']
        result.append(analyse_result)
    return result
